<template>
  <div class="mk_mawen">
     <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="账号密码登录" name="first">
            <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
<!-- 
         <el-form-item label="验证码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="$router.history.push('/register')">注册</el-button>
      </el-form-item>
    </el-form>
    </el-tab-pane>
  </el-tabs>
    
  </div>
</template>

<script>
import { getusers } from "@/api";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var username = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    return {
      activeName: 'first',
      ruleForm: {
        pass: "",
        username: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        username: [{ validator: username, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          getusers(this.ruleForm).then((res) => {
            if (res.data.length) {
              const h = this.$createElement;
              this.$notify({
                title: "标题名称",
                duration: 3000,
                message: h("i", { style: "color: teal" }, "3秒跳转首页"),
              });
              setTimeout(() => {
                this.$router.history.push("/");
              }, 3000);
              localStorage.setItem("user",JSON.stringify(res.data[0]));
              localStorage.setItem("flag",JSON.stringify(true))
            }else{
                alert("用户名或密码错误")
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClick(tab, event) {
        console.log(tab, event);
      }
  },
};
</script>

<style scoped>


</style>
<style>
.mk_mawen .el-button+.el-button{
  margin-left: 66px;
}
  .mk_mawen .el-tabs__nav{
  width: 95%;
  margin-left:40%;
}
.mk_mawen .el-input{
  width: 90%;
}
.mk_mawen .el-form-item__label{
  border-bottom: 1px solid #000;
  margin-left: 10px;
}
.mk_mawen .el-input__inner{
  border: 0;
  border-bottom: 1px solid #000;
  border-radius: 0;
}
.mk_mawen .el-form-item__label{
  padding: 0px 25px 0px 0px;
}
</style>
